/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SettingsDialog.java
 *
 * Created on 10-11-2008, 21:12:32
 */

package dk.znz.comm;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Esben
 */
public class SettingsDialog extends javax.swing.JDialog {

    /** Creates new form SettingsDialog */
    public SettingsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private DefaultComboBoxModel getParityModel() {
        EnumModelEntry[] objects = new EnumModelEntry[] {
            new EnumModelEntry(SerialPort.PARITY_NONE, "None"),
            new EnumModelEntry(SerialPort.PARITY_ODD, "Odd"),
            new EnumModelEntry(SerialPort.PARITY_EVEN, "Even"),
            new EnumModelEntry(SerialPort.PARITY_MARK, "Mark"),
            new EnumModelEntry(SerialPort.PARITY_SPACE, "Space")
        };
        return new DefaultComboBoxModel(objects);
    }

    private DefaultComboBoxModel getDataBitsModel() {
        EnumModelEntry[] objects = new EnumModelEntry[] {
            new EnumModelEntry(SerialPort.DATABITS_5, "5 data bits"),
            new EnumModelEntry(SerialPort.DATABITS_6, "6 data bits"),
            new EnumModelEntry(SerialPort.DATABITS_7, "7 data bits"),
            new EnumModelEntry(SerialPort.DATABITS_8, "8 data bits")
        };
        return new DefaultComboBoxModel(objects);
    }

    private DefaultComboBoxModel getStopBitsModel() {
        EnumModelEntry[] objects = new EnumModelEntry[] {
            new EnumModelEntry(SerialPort.STOPBITS_1, "1 stop bit"),
            new EnumModelEntry(SerialPort.STOPBITS_1_5, "1.5 stop bits"),
            new EnumModelEntry(SerialPort.STOPBITS_2, "2 stop bits")
        };
        return new DefaultComboBoxModel(objects);
    }

    private DefaultComboBoxModel getFlowcontrolModel() {
        EnumModelEntry[] objects = new EnumModelEntry[] {
            new EnumModelEntry(SerialPort.FLOWCONTROL_NONE, "None"),
                    new EnumModelEntry(SerialPort.FLOWCONTROL_RTSCTS_IN, "RTSCTS_IN"),
                    new EnumModelEntry(SerialPort.FLOWCONTROL_RTSCTS_OUT, "RTSCTS_OUT"),
                    new EnumModelEntry(SerialPort.FLOWCONTROL_XONXOFF_IN, "XONXOFF_IN"),
                    new EnumModelEntry(SerialPort.FLOWCONTROL_XONXOFF_OUT, "XONXOFF_OUT")
        };
        return new DefaultComboBoxModel(objects);
    }

    private DefaultComboBoxModel getCharsetModel() {
        return new DefaultComboBoxModel(Charset.availableCharsets().keySet().toArray(new String[0]));
    }

    private void updateCommPorts(DefaultComboBoxModel modelToUpdate) {
        Enumeration<CommPortIdentifier> e = CommPortIdentifier.getPortIdentifiers();
        CommPortIdentifier[] commPortIdentifiers = Collections.list(e).toArray(new CommPortIdentifier[0]);
        modelToUpdate.removeAllElements();
        for (int i = 0; i < commPortIdentifiers.length; i++) {
            CommPortIdentifier commPortIdentifier = commPortIdentifiers[i];
            modelToUpdate.addElement(commPortIdentifier.getName());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel8 = new javax.swing.JLabel();
        configurationPanel = new java.awt.Panel();
        portLabel = new javax.swing.JLabel();
        portComboBox = new javax.swing.JComboBox();
        baudRateLabel = new javax.swing.JLabel();
        baudRateComboBox = new javax.swing.JComboBox();
        dataBitsLabel = new javax.swing.JLabel();
        dataBitsComboBox = new javax.swing.JComboBox();
        parityLabel = new javax.swing.JLabel();
        parityComboBox = new javax.swing.JComboBox();
        stopBitsLabel = new javax.swing.JLabel();
        stopBitsComboBox = new javax.swing.JComboBox();
        flowControlLabel = new javax.swing.JLabel();
        flowControlComboBox = new javax.swing.JComboBox();
        charsetLabel = new javax.swing.JLabel();
        charsetComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dk.znz.comm.SerialCommApp.class).getContext().getResourceMap(SettingsDialog.class);
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        configurationPanel.setName("configurationPanel"); // NOI18N
        configurationPanel.setLayout(new java.awt.GridBagLayout());

        portLabel.setText(resourceMap.getString("portLabel.text")); // NOI18N
        portLabel.setName("portLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(portLabel, gridBagConstraints);

        portComboBox.setEditable(true);
        portComboBox.setModel(new DefaultComboBoxModel());
        portComboBox.setName("portComboBox"); // NOI18N
        updateCommPorts((DefaultComboBoxModel)portComboBox.getModel());
        portComboBox.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                portComboBoxPopupMenuWillBecomeVisible(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(portComboBox, gridBagConstraints);

        baudRateLabel.setText(resourceMap.getString("baudRateLabel.text")); // NOI18N
        baudRateLabel.setName("baudRateLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(baudRateLabel, gridBagConstraints);

        baudRateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "600", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "56000", "57600", "115200", "128000", "256000" }));
        baudRateComboBox.setName("baudRateComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(baudRateComboBox, gridBagConstraints);

        dataBitsLabel.setText(resourceMap.getString("dataBitsLabel.text")); // NOI18N
        dataBitsLabel.setName("dataBitsLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(dataBitsLabel, gridBagConstraints);

        dataBitsComboBox.setModel(getDataBitsModel());
        dataBitsComboBox.setName("dataBitsComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(dataBitsComboBox, gridBagConstraints);

        parityLabel.setText(resourceMap.getString("parityLabel.text")); // NOI18N
        parityLabel.setName("parityLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(parityLabel, gridBagConstraints);

        parityComboBox.setModel(getParityModel());
        parityComboBox.setName("parityComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(parityComboBox, gridBagConstraints);

        stopBitsLabel.setText(resourceMap.getString("stopBitsLabel.text")); // NOI18N
        stopBitsLabel.setName("stopBitsLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(stopBitsLabel, gridBagConstraints);

        stopBitsComboBox.setModel(getStopBitsModel());
        stopBitsComboBox.setName("stopBitsComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(stopBitsComboBox, gridBagConstraints);

        flowControlLabel.setText(resourceMap.getString("flowControlLabel.text")); // NOI18N
        flowControlLabel.setName("flowControlLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(flowControlLabel, gridBagConstraints);

        flowControlComboBox.setModel(getFlowcontrolModel());
        flowControlComboBox.setName("flowControlComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(flowControlComboBox, gridBagConstraints);

        charsetLabel.setText(resourceMap.getString("charsetLabel.text")); // NOI18N
        charsetLabel.setName("charsetLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 4);
        configurationPanel.add(charsetLabel, gridBagConstraints);

        charsetComboBox.setModel(getCharsetModel());
        charsetComboBox.setName("charsetComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        configurationPanel.add(charsetComboBox, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(configurationPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap(175, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(configurationPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(108, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void portComboBoxPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_portComboBoxPopupMenuWillBecomeVisible
        // FIXME
        //updateCommPorts((DefaultComboBoxModel)comPortComboBox.getModel());
}//GEN-LAST:event_portComboBoxPopupMenuWillBecomeVisible

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox baudRateComboBox;
    private javax.swing.JLabel baudRateLabel;
    private javax.swing.JComboBox charsetComboBox;
    private javax.swing.JLabel charsetLabel;
    private java.awt.Panel configurationPanel;
    private javax.swing.JComboBox dataBitsComboBox;
    private javax.swing.JLabel dataBitsLabel;
    private javax.swing.JComboBox flowControlComboBox;
    private javax.swing.JLabel flowControlLabel;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JComboBox parityComboBox;
    private javax.swing.JLabel parityLabel;
    private javax.swing.JComboBox portComboBox;
    private javax.swing.JLabel portLabel;
    private javax.swing.JComboBox stopBitsComboBox;
    private javax.swing.JLabel stopBitsLabel;
    // End of variables declaration//GEN-END:variables

}
